import buildingApi from '@/config/server/api/building'
import biApi from '@/config/server/api/bi'
import customerApi from '@/config/server/api/customer'
import orderApi from '@/config/server/api/order'
import tool from '@/common/tool.js'
import moment from 'moment'
import dt from '@dt/dt'

function logGenerate(data) {
  console.log(dt)
  return buildingApi.daily.log.fetch({
    ...data,
    sourceType: 'web',
    buildingId: dt.buildInfo().housesId,
    userId: dt.userInfo().userId
  })
}

/*
 * 小结
 * */

//根据日期和楼盘查询小节详情
function findDailySection(params) {
  return buildingApi.daily.summary.findDailySection.fetch(params).then((res) => {
    return res
  })
}

//新增小结
function saveDailySection(params) {
  return buildingApi.daily.summary.saveDailySection.fetch(params).then((res) => {
    return res
  })
}

//编辑小结
function updateDailySection(params) {
  return buildingApi.daily.summary.updateDailySection.fetch(params).then((res) => {
    return res
  })
}

//删除小结
function deleteDailySection(params) {
  return buildingApi.daily.summary.deleteDailySection.fetch(params).then((res) => {
    return res
  })
}

/*
 * 模板
 * */
//根据楼盘查询模版配置
function getModelList(params) {
  return buildingApi.daily.template.getModelList.fetch(params).then((res) => {
    return res
  })
}
//根据楼盘查询销售统计模版配置
function getModelListForApp(params) {
  return buildingApi.daily.template.getModelListForApp.fetch(params).then((res) => {
    return res
  })
}

//编辑模版配置
function updateModelList(params) {
  return buildingApi.daily.template.updateModelList.fetch(params).then((res) => {
    return res
  })
}

//获取销售指标列表
function findDailySales(params) {
  return buildingApi.daily.template.findDailySales.fetch(params).then((res) => {
    return res
  })
}

//销售指标列表排序
function sortDailySales(params) {
  return buildingApi.daily.template.sortDailySales.fetch(params).then((res) => {
    return res
  })
}

/*
 * 竞品 ===== 联代
 * */

//当前案场联代-竞品集合
function findDailyGenerationList(params) {
  return buildingApi.daily.generation.findDailyGenerationList.fetch(params).then((res) => {
    return res
  })
}

//更改名字查询当前名称是否已经存在,true代表已存在，不可插入 fasle代表不存在，可插入
function checkDailyGeneration(params) {
  return buildingApi.daily.generation.checkDailyGeneration.fetch(params).then((res) => {
    return res
  })
}

//新增时获取历史联代-竞品集合，排除传进来id
function findDailyGenerationHistory(params) {
  return buildingApi.daily.generation.findDailyGenerationHistory.fetch(params).then((res) => {
    return res
  })
}

//保存联代-竞品
function updateDailyGeneration(params) {
  return buildingApi.daily.generation.updateDailyGeneration.fetch(params).then((res) => {
    return res
  })
}

//删除联代-竞品 (只有当删除的数据有id才会删除)
function deleteDailyGenerationById(params) {
  return buildingApi.daily.generation.deleteDailyGenerationById.fetch(params).then((res) => {
    return res
  })
}

//删除联代-竞品整个模块
function deleteDailyGeneration(params) {
  return buildingApi.daily.generation.deleteDailyGeneration.fetch(params).then((res) => {
    return res
  })
}

/*
 * 渠道编辑
 * */

//根据楼盘查询显示-未显示渠道列表
function findDailyChannel(params) {
  return buildingApi.daily.dailyChannel.findDailyChannel.fetch(params).then((res) => {
    return res
  })
}
//编辑渠道
function updateLockDailyChannel(params) {
  return buildingApi.daily.dailyChannel.updateLockDailyChannel.fetch(params).then((res) => {
    return res
  })
}

/*
 * 重点客户
 *
 * */

//配置当日到访客户列表
function checkInCustomerLists(params) {
  return customerApi.daily.checkInCustomerLists.fetch(params).then((res) => {
    return res
  })
}

//获取当日重点客户列表
function configCustomerList(params) {
  return customerApi.daily.configCustomerList.fetch(params).then((res) => {
    return res
  })
}
//勾选-取消勾选客户 加锁-解锁
function updateDailyCustomer(params) {
  return buildingApi.daily.updateDailyCustomer.fetch(params).then((res) => {
    return res
  })
}

/*
 * 获取页面数据
 * */
//pk数据列表
function pkDataList(params) {
  return biApi.daily.pkDataList.fetch(params).then((res) => {
    return res
  })
}

//客户统计
function customerStatistics(params) {
  return biApi.daily.customerStatistics.fetch(params).then((res) => {
    return res
  })
}

//销售统计
function orderSales(params) {
  return biApi.daily.orderSales.fetch(params).then((res) => {
    return res
  })
}
//日报聚合接口：PK、客户、渠道、销售
function dailyPaperMerge(params) {
  return biApi.daily.dailyPaperMerge.fetch(params).then((res) => {
    return res
  })
}

function updateTask(params) {
  return customerApi.task.updateTask.fetch(params).then((res) => {
    return res
  })
}
function getCurrentMonthAndNextMonth(params) {
  return customerApi.daily.getCurrentMonthAndNextMonth.fetch(params)
}
function getCurrentMonthAndNextMonth1(params) {
  return orderApi.setBuildTarget.build.getCurrentMonthAndNextMonth1.fetch(params)
}

function findDailyGenerationAllocation(params) {
  return buildingApi.daily.findDailyGenerationAllocation.fetch(params)
}

function findDailyGenerationAllocationHomePage(params) {
  return buildingApi.daily.findDailyGenerationAllocationHomePage.fetch(params)
}

function getAllTargetMsg(params) {
  return biApi.daily.getAllTargetMsg.fetch(params)
}
function getBuildInfo(params) {
  return buildingApi.caseList.buildinghousesList.fetch(params)
}
function resovleTime(type, date) {
  let chooseTime = {}
  // type 0 昨日 1 今日 2本周 3本月 4本年
  let time = date ? new Date(date) : new Date()
  switch (type) {
    case 0:
      time.setTime(time.getTime() - 24 * 60 * 60 * 1000)
      var curTime =
        time.getFullYear() +
        '-' +
        (time.getMonth() + 1 > 9 ? time.getMonth() + 1 : '0' + (time.getMonth() + 1)) +
        '-' +
        (time.getDate() > 9 ? time.getDate() : '0' + time.getDate())
      chooseTime = {
        startTime: curTime,
        endTime: curTime
      }
      break
    case 1:
      time.setTime(time.getTime())
      var curTime =
        time.getFullYear() +
        '-' +
        (time.getMonth() + 1 > 9 ? time.getMonth() + 1 : '0' + (time.getMonth() + 1)) +
        '-' +
        (time.getDate() > 9 ? time.getDate() : '0' + time.getDate())
      chooseTime = {
        startTime: curTime,
        endTime: curTime
      }
      break
    case 2:
      chooseTime = {
        startTime: moment(time).startOf('isoWeek').format('YYYY-MM-DD'),
        endTime: moment(time).endOf('isoWeek').format('YYYY-MM-DD')
      }
      break
    case 3:
      chooseTime = {
        startTime: moment(time).startOf('month').format('YYYY-MM-DD'),
        endTime: moment(time).endOf('month').format('YYYY-MM-DD')
      }
      break
    case 4:
      var startTime = time.getFullYear() + '-01-01'
      var endTime = time.getFullYear() + '-12-31'
      chooseTime = {
        startTime: startTime,
        endTime: endTime
      }
      break
    default:
      break
  }
  return chooseTime
}

export default {
  findDailySection,
  saveDailySection,
  updateDailySection,
  deleteDailySection,

  getModelList,
  getModelListForApp,
  updateModelList,
  findDailySales,
  sortDailySales,

  findDailyGenerationList,
  checkDailyGeneration,
  findDailyGenerationHistory,
  updateDailyGeneration,
  deleteDailyGenerationById,
  deleteDailyGeneration,

  findDailyChannel,
  updateLockDailyChannel,

  checkInCustomerLists,
  configCustomerList,
  updateDailyCustomer,

  pkDataList,
  customerStatistics,
  orderSales,
  dailyPaperMerge,
  resovleTime,

  logGenerate,
  updateTask,
  getCurrentMonthAndNextMonth,
  getCurrentMonthAndNextMonth1,
  findDailyGenerationAllocation,
  findDailyGenerationAllocationHomePage,
  getAllTargetMsg,
  getBuildInfo
}
